<?php

namespace app\api\controller;

use app\common\controller\BasicApi;
use app\common\model\UserModel;
use WeMini\Crypt;

class Member extends BasicApi
{
    protected $member;
    protected $openid;

    public function _initialize()
    {
        parent::_initialize();
        $this->openid = isset($this->header['token'])?$this->header['token']:'';
    }

    /**
     * 用户登录
     */
    public function userLogin(){
        $wechat_mini = config('wechat.miniapp');
        $config['appid'] = $wechat_mini['appid'];
        $config['appsecret'] = $wechat_mini['appsecret'];
        $code = $this->request->get('code');//code
        $crypt = new Crypt($config);
        $result = $crypt->session($code);
        $openid = $result['openid'];
        $session_key = $result['session_key'];
        if (isset($openid) && !empty($openid)) {
            $this->success('获取openid成功',['openid'=>$openid,'session_key'=>$session_key],200);
        }else{
            $this->error('获取openid异常');
        }
    }

    /**
     * 获取会员信息
     */
    protected function getMember()
    {
        return UserModel::where(['openid'=>$this->openid])->find();
    }
}